﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DGRobot
{
    public partial class WelcomeFrm : Form
    {
        public WelcomeFrm()
        {
            InitializeComponent();
            StartRobot();
        }
        public void StartRobot()
        {
            RobotDriver.Instance.OnSpeakStart = RobotDriver_OnSpeakStart;
            RobotDriver.Instance.OnListenStart = RobotDriver_OnListenStart;
            RobotDriver.Instance.OnSpeakCompleted = RobotDriver_OnSpeakCompleted;
            RobotDriver.Instance.OnListening = RobotDriver_OnListening;
            RobotDriver.Instance.OnListenCompleted = RobotDriver_OnListenCompleted;
        }
        public void StopRobot()
        {
            RobotDriver.Instance.StopSpeak(false);
            RobotDriver.Instance.StopListen();
        }
        private void RobotDriver_OnListenCompleted(RobotDriver.ListenCompletedEventArgs args)
        {
            var hearWord = args.HearTheWord;
            bool has = true;
            var wakeups = Config.WakeupWord.Split('|');
            foreach (var wakeup in wakeups)
            {
                has = true;
                if (string.IsNullOrEmpty(wakeup)) continue;
               
                foreach (var word in wakeup)
                {
                    if (!hearWord.Contains(word))
                    {
                        has = false;
                        break;
                    }
                }
                if (has) break;
            }
            if (!has)
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(1000);
                    RobotDriver.Instance.StartListen(nameof(WelcomeFrm));
                });
            }
            else
            {
                Invoke(new MethodInvoker(() =>
                {
                    this.Close();
                }));
            }
            
        }

        private void RobotDriver_OnListening(RobotDriver.ListeningEventArgs args)
        {
            Invoke(new MethodInvoker(() =>
            {
                this.lblWords.Text += args.HearTheWord;
            }));
        }

        private void RobotDriver_OnSpeakCompleted(RobotDriver.SpeakCompletedEventArgs args)
        {
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1000);
                RobotDriver.Instance.StartListen(nameof(WelcomeFrm));
            });
        }

        private void RobotDriver_OnListenStart(RobotDriver.ListenStartEventArgs args)
        {
            Invoke(new MethodInvoker(() =>
            {
                this.lblWords.Text = "";
                this.picRobotState.Image = global::DGRobot.Properties.Resources.record;
                this.picRobotState.Tag = "listen";
            }));
        }

        private void RobotDriver_OnSpeakStart(RobotDriver.SpeakStartEventArgs args)
        {
            Invoke(new MethodInvoker(() =>
            {
                this.picRobotState.Image = global::DGRobot.Properties.Resources.speak;
                this.picRobotState.Tag = "speak";
            }));
        }


        private void Form1_Click(object sender, EventArgs e)
        {
            OpenSvcFrm();
        }

        private void OpenSvcFrm()
        {
            //_mainFrm.GoForm(RouteHelper.GetForm(nameof(SelectServiceFrm), () => new SelectServiceFrm()));
            this.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenSvcFrm();
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void splitContainer1_Panel2_Click(object sender, EventArgs e)
        {
            OpenSvcFrm();
        }

        private void WelcomeFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            StopRobot();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            var tag = this.picRobotState.Tag.ToString();
            if (tag == "speak")
            {
                RobotDriver.Instance.StopSpeak();
                RobotDriver.Instance.StartListen(nameof(WelcomeFrm));
            }
            else
            {
                RobotDriver.Instance.StopListen();

                RobotDriver.Instance.StartListen(nameof(WelcomeFrm));
            }
        }

        private void WelcomeFrm_Load(object sender, EventArgs e)
        {
            //RobotDriver.Instance.StartListen(nameof(WelcomeFrm));
            RobotDriver.Instance.StartSpeak(nameof(WelcomeFrm),"Hello,我是盾构先生!");
        }
    }
}
